package com.hunan.api.catv.mq.consumer;


import com.google.gson.JsonObject;
import com.hunan.api.catv.entity.enums.Channel;
import com.hunan.api.catv.entity.enums.TpnsAudienceType;
import com.hunan.api.catv.mq.MqConstants;
import com.hunan.api.catv.service.biz.tencent_cloud.push.PushBiz;
import com.hunan.api.catv.utils.GsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class SubscribeNotifyConsumer {

    public static Logger log = LoggerFactory.getLogger(SubscribeNotifyConsumer.class);

    @Autowired
    PushBiz pushBiz;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqConstants.subscribe_notify_queue, durable = "true"), exchange = @Exchange(value = MqConstants.subscribe_notify_exchange, type = "x-delayed-message", arguments = @Argument(name = "x-delayed-type", value = "direct")), key = MqConstants.subscribe_notify_routingkey))
    public void notify(String msg) {
        log.info("[收到预约推送消息]:{}", msg);
        try {
            JsonObject msgJson = GsonUtils.fromJson(msg, JsonObject.class);
            Integer contentType = msgJson.get("contentType").getAsInt();
            String content = msgJson.get("content").getAsString();
            pushBiz.push(TpnsAudienceType.token_list.getCode(), Channel.Android.getCode(), contentType, content);
            pushBiz.push(TpnsAudienceType.token_list.getCode(), Channel.IOS.getCode(), contentType, content);
        } catch (Exception e) {
            log.error("预约推送Error:{}", e);
        }
    }
}
